//
//  AboutOurVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/9.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AboutOurVC.h"
#import <SafariServices/SafariServices.h>
#import "CJWKWebViewVC.h"
#import "CJAppInfo.h"
#import "MyInPutAlertView.h"

@interface AboutOurVC ()<SFSafariViewControllerDelegate>
@property (nonatomic,copy) NSArray *arrTitles;
@property (nonatomic,copy) NSArray *arrContent;
@property (nonatomic,strong) UIImageView *imgVHead;
@property (nonatomic,strong) CJLabel *labTitle;
@property (nonatomic,strong) CJLabel *labVersion;
@property (nonatomic,strong) CJLabel *labBottom;
@property (nonatomic,strong) CJLabel *labBottomContent;
@property (nonatomic,copy) NSString *strWenUrl;
@property (nonatomic,copy) NSString *strPhone;
@property (weak, nonatomic) IBOutlet UIButton *btnFuwuXieyi;
@property (weak, nonatomic) IBOutlet UIButton *btnYinSiXieyi;
@end

@implementation AboutOurVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"关于我们")];
    [self setBackLeftButton:@""];
//    [self setSecondAppNavigationBarStyle];
    [self.view addSubview:self.imgVHead];
    [self.view addSubview:self.labTitle];
    [self.view addSubview:self.labVersion];
    [self.view addSubview:self.labBottom];
    [self.view addSubview:self.labBottomContent];
    [self.btnFuwuXieyi setTitleColor:[UIColor cjMainColor] forState:UIControlStateNormal];
    [self.btnYinSiXieyi setTitleColor:[UIColor cjMainColor] forState:UIControlStateNormal];
    self.strPhone = @"";
    self.strWenUrl = @"";
    [self laodData];
 
#ifdef IsDebugDevelopmentModel
    self.imgVHead.userInteractionEnabled = YES;
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DoubleTap:)];
    gestureRecognizer.numberOfTapsRequired = 5;
    [self.imgVHead addGestureRecognizer:gestureRecognizer];
#else
    
#endif

}

-(void)DoubleTap:(UITapGestureRecognizer*)recognizer{

//处理双击操作
    DLog(@"========处理双击操作");
    WS(ws);
    MyInPutAlertView *alert = [[MyInPutAlertView alloc]initWithTitle:@"更改网址" Placeholder:CJBaseUrl ClickOnSure:^(NSString *strInPut) {
        
        CJSaveMyDefault(CJMainURLChange, strInPut);
        [ws laodData];
    } ClickOnCancle:^(NSString *strCancle) {
        
    }];

    alert.center = KEYWINDOW.center;
    WLAlertView * alertView = [[WLAlertView alloc]init];
    alertView.containerView = alert;
    alertView.isTouchOtherUndissmiss=YES;
    [alertView show];
}

- (void)laodData{

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/partyGroup/aboutUs/%@",CJBaseUrl,[CJControl getBranchCode]);
    WS(ws);
    NSDictionary *dic = [[NSDictionary alloc]init];
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dic success:^(id  _Nonnull responseObject) {
        
//        DLog(@"++++++++%@",responseObject);
//        _arrContent = @[[CJRegular getIsStrEmpty:responseObject[@"wwwUrl"]] ? @"":responseObject[@"wwwUrl"],[CJRegular getIsStrEmpty:responseObject[@"mobilePhone"]] ? @"":responseObject[@"mobilePhone"]];
        _arrContent = @[[CJRegular getIsStrEmpty:responseObject[@"mobilePhone"]] ? @"":responseObject[@"mobilePhone"]];

        [ws.imgVHead sd_setImageWithURL:[NSURL URLWithString:[CJControl getThreeUnaryValueStr:responseObject[@"shipmentLogoPath"]]] placeholderImage:[UIImage imageNamed:@"zcj_default_person_icon"]];
        ws.labTitle.text = [CJControl getThreeUnaryValueStr:responseObject[@"shipmentAppName"]];
        ws.labBottom.text = [CJControl getThreeUnaryValueStr:responseObject[@"ownerName"]];
        ws.labBottomContent.text = [CJControl getThreeUnaryValueStr:responseObject[@"copyrightName"]];
        ws.strWenUrl = [CJControl getThreeUnaryValueStr:responseObject[@"wwwUrl"]];
        ws.strPhone = [CJControl getThreeUnaryValueStr:responseObject[@"mobilePhone"]];
        [ws createUI];
        
    } failure:^(NSError * _Nonnull error) {
//        DLog(@"++++++++%@",error);
    }];
}

- (void)createUI{
    
    _arrTitles = @[@"服务热线"]; //@"网站地址",

    for (int i = 0; i < self.arrTitles.count; i ++) {
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0, self.labTitle.bottom + self.imgVHead.height/2 + i * LW(50), SCREEN_WIDTH, LW(50))];
    
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBackView:)];
        liebiaoView.tag = i+100;
        liebiaoView.userInteractionEnabled = YES;
        [liebiaoView addGestureRecognizer:tap];
       
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, LW(5), LW(120), LW(40))];
        label.text = self.arrTitles[i];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont cjTitleFont15];
        label.textColor = [UIColor cjMainTextColor];
        [liebiaoView addSubview:label];
        
        CJLabel *contentLabel = CJLabel.labInit
        .labFrame(CGRectMake(label.right, label.top, SCREEN_WIDTH - CJToLeftMargin-label.width - LW(25), LW(40)))
        .labTextColor([UIColor cjSubheadTextColor])
        .labAlignment(NSTextAlignmentRight)
        .labText(self.arrContent[i])
        ;
        [liebiaoView addSubview:contentLabel];
        
        UIImageView * right = [[UIImageView alloc]initWithFrame:CGRectMake(contentLabel.right + 5,LW(20), LW(6), LW(10))];
        right.image = [UIImage imageNamed:@"zcj_arrow_right"];
        right.contentMode = UIViewContentModeScaleAspectFit;
        [liebiaoView addSubview:right];

        if (i == 0) {
            UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
            bg.backgroundColor = [UIColor cjBackgroundColor];
            [liebiaoView addSubview:bg];
        }
    }
}

/// 服务协议
/// @param sender <#sender description#>
- (IBAction)btnUserXieYiAction:(UIButton *)sender {
    
    //    0716 release0716-agreement-newhtml 修改协议地址传参agreementType：协议类型:1=司机端隐私协议/2=司机端用户协议/3=司机端注销协议/4=司机端物流协议/5=接单协议/6=司机端SDK协议/7=货主端隐私协议/8=货主端用户协议/9=货主端注销协议/10=货主端物流协议/11=货主端SDK协议/12=PC端隐私协议/13=PC端用户协议/14=PC端物流协议/15=网商协议  例如 /agreement/agreement.html?agreementType=1
        
    CJWKWebViewVC *vc = [CJWKWebViewVC new];
    vc.strTitle = @"服务协议";
    vc.useUTF8 = YES;
    vc.strUrl = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=8",CJBaseUrl);
    [self pushVc:vc];
}

/// 隐私协议
/// @param sender <#sender description#>
- (IBAction)btnYinSiZhengCeAction:(UIButton *)sender {
    //    0716 release0716-agreement-newhtml 修改协议地址传参agreementType：协议类型:1=司机端隐私协议/2=司机端用户协议/3=司机端注销协议/4=司机端物流协议/5=接单协议/6=司机端SDK协议/7=货主端隐私协议/8=货主端用户协议/9=货主端注销协议/10=货主端物流协议/11=货主端SDK协议/12=PC端隐私协议/13=PC端用户协议/14=PC端物流协议/15=网商协议  例如 /agreement/agreement.html?agreementType=1
    CJWKWebViewVC *vc = [CJWKWebViewVC new];
    vc.strTitle = @"隐私协议";
    vc.useUTF8 = YES;
    vc.strUrl = CJStringWithFormat(@"%@/agreement/agreement.html?agreementType=7",CJBaseUrl);
    [self pushVc:vc];
}

- (void)OnTapBackView:(UITapGestureRecognizer *)gesture {
    

//    if (gesture.view.tag == 100) {
////        SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:CJStringWithFormat(@"http://%@",self.strWenUrl)] entersReaderIfAvailable:YES];
////        safariVC.delegate = self;
////        [self presentViewController:safariVC animated:YES completion:nil];
//        CJWKWebViewVC *vc = [CJWKWebViewVC new];
//        vc.strUrl = self.strWenUrl; //; @"http://114.116.240.176/trackOfVehicle?vehicleCode=9343b1c81dee41edb09ab287133755ba&vehicleNumber=%E4%BA%ACA88888";  //self.strWenUrl;
//        vc.strTitle = @"";
//        [self pushVc:vc];
//    }else if(gesture.view.tag == 101){
        
//        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:CJStringWithFormat(@"tel:%@",self.strPhone)]];
        [CJControl clickPhoneWithPhoneNum:self.strPhone];
//    }
}

-(void)showAlertViewTitle:(NSString *)title subTitle:(NSString *)subTitle openUrl:(NSString *)openUrl{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl] options:@{} completionHandler:^(BOOL success) {
                    
                }];
            } else {
                BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl]];
                NSLog(@"Open  %d",success);
            }
            
        } else{
            bool can = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:openUrl]];
            if(can){
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl]];
            }
        }
    }];
    [alertVC addAction:cancel];
    [alertVC addAction:sure];
    [self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark --- lazy---


//-(NSArray *)arrTitles{
//    if(_arrTitles){
//        _arrTitles = @[@"给我评价",@"当前版本",@"法律条例与隐私协议",@"检测新版本"];
//    }
//    return _arrTitles;
//}

//-(NSArray *)arrContent{
//    if(_arrContent){
//        _arrContent = @[@"",@"1.0",@"",@""];
//    }
//    return _arrContent;
//}

- (UIImageView *)imgVHead{
    
    if(!_imgVHead){
        _imgVHead = [UIImageView imageViewWithFrame:CGRectMake(SCREEN_WIDTH/2 - LW(40), kTopHeight+ LW(40), LW(80), LW(80)) backgroundColor:nil borderColor:nil cornerRadius:5 image:UIImageNamed(@"zcj_default_person_icon") contentMode:@(0)];
        [CJUtil getUIImageVCutAllCornRound:_imgVHead height:5];
    }
    return _imgVHead;
}

- (CJLabel *)labTitle{
    if (!_labTitle) {
        _labTitle = CJLabel.labInit
        .labFrame(CGRectMake(0, _imgVHead.bottom + 5, SCREEN_WIDTH, LW(30)))
        .labTextColor([UIColor cjMainTextColor])
        .labText(@"")
        .labAlignment(NSTextAlignmentCenter)
        ;
    }
    return _labTitle;
}

- (CJLabel *)labVersion{
    if (!_labVersion) {
        _labVersion = CJLabel.labInit
        .labFrame(CGRectMake(0, _labTitle.bottom , SCREEN_WIDTH, LW(15)))
        .labTextColor([UIColor cjMainTextColor])
        .labText(CJStringWithFormat(@"V%@",[CJAppInfo appVersion]))
        .labAlignment(NSTextAlignmentCenter)
        ;
    }
    return _labVersion;
}

- (CJLabel *)labBottom{
    if (!_labBottom) {
        _labBottom = CJLabel.labInit
        .labFrame(CGRectMake(0,kScreenHeight - LW(55), kScreenWidth, LW(20)))
        .labTextColor([UIColor cjMainTextColor])
        .labFontSize(13)
        .labText(@"")  //kTMS物流 版权所有 \n Copyright@kTMS物流/All Right Reserved
        .labAlignment(NSTextAlignmentCenter)
        ;
    }
    return _labBottom;
}
- (CJLabel *)labBottomContent{
    if (!_labBottomContent) {
        _labBottomContent = CJLabel.labInit
        .labFrame(CGRectMake(0,_labBottom.bottom, kScreenWidth, _labBottom.height))
        .labTextColor([UIColor cjMainTextColor])
        .labFontSize(13)
        .labText(@"")  //kTMS物流 版权所有 \n Copyright@kTMS物流/All Right Reserved
        .labAlignment(NSTextAlignmentCenter)
        ;
    }
    return _labBottomContent;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
